package com.funsport.controller.mobile;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funsport.common.Result;
import com.funsport.dto.request.VenueBookingRequest;
import com.funsport.dto.response.AvailableTimeSlot;
import com.funsport.dto.response.VenueBookingResponse;
import com.funsport.service.IVenueBookingService;
import com.funsport.security.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 移动端场地预订Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/mobile/venue/booking")
@RequiredArgsConstructor
@Tag(name = "移动端场地预订")
public class MobileVenueBookingController {
    
    private final IVenueBookingService venueBookingService;
    
    @Operation(summary = "创建预订")
    @PostMapping("/create")
    public Result<Long> createBooking(@Valid @RequestBody VenueBookingRequest request) {
        Long userId = SecurityUtils.getCurrentUserId();
        Long bookingId = venueBookingService.createBooking(request, userId);
        return Result.success("预订成功", bookingId);
    }
    
    @Operation(summary = "获取我的预订列表")
    @GetMapping("/my")
    public Result<Page<VenueBookingResponse>> getMyBookings(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String status
    ) {
        Long userId = SecurityUtils.getCurrentUserId();
        Page<VenueBookingResponse> bookings = venueBookingService.getUserBookings(userId, page, pageSize, status);
        return Result.success(bookings);
    }
    
    @Operation(summary = "获取预订详情")
    @GetMapping("/{id}")
    public Result<VenueBookingResponse> getBookingDetail(@PathVariable Long id) {
        VenueBookingResponse booking = venueBookingService.getBookingDetail(id);
        return Result.success(booking);
    }
    
    @Operation(summary = "获取场地可用时间段")
    @GetMapping("/venue/{venueId}/available")
    public Result<List<AvailableTimeSlot>> getAvailableTimeSlots(
            @PathVariable Long venueId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date
    ) {
        List<AvailableTimeSlot> slots = venueBookingService.getAvailableTimeSlots(venueId, date);
        return Result.success(slots);
    }
    
    @Operation(summary = "取消预订")
    @PostMapping("/{id}/cancel")
    public Result<Void> cancelBooking(@PathVariable Long id) {
        Long userId = SecurityUtils.getCurrentUserId();
        venueBookingService.cancelBooking(id, userId);
        return Result.success("预订已取消", null);
    }
}

